package cn.smbms.service.user;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import cn.smbms.dao.user.UserMapper;
import cn.smbms.pojo.User;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
	
	@Resource
	private UserMapper userMapper;
	/**
	 * 增加用户信息
	 *
	 * @param user
	 * @return
	 */
	@Override
	public boolean add(User user) {
		boolean flag = false;
		try {
			if (user != null) {
				int updateRows = userMapper.add(user);
				if (updateRows>0){
					flag=true;
				}
			}else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public User login(String userCode, String userPassword) throws Exception {
		// TODO Auto-generated method stub
		User user = null;
		user = userMapper.getLoginUser(userCode);
		//匹配密码
		if(null != user){
			if(!user.getUserPassword().equals(userPassword))
				user = null;
		}
		return user;
	}

	/**
	 * 根据条件查询用户列表
	 *
	 * @param queryUserName
	 * @param queryUserRole
	 * @param currentPageNo
	 * @param pageSize
	 * @return
	 */
	@Override
	public List<User> getUserList(String queryUserName, Integer queryUserRole, Integer currentPageNo, Integer pageSize) {
		List<User> userList = new ArrayList<User>();
		try {
			if(queryUserName!=null){
				queryUserName="%"+queryUserName+"%";
			}else {
				queryUserName=null;
			}
			if(currentPageNo!=null){
				currentPageNo=(currentPageNo-1)*pageSize;
			}
			userList = userMapper.getUserList(queryUserName,queryUserRole,currentPageNo,pageSize);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return userList;
	}

	/**
	 * 根据条件查询用户表记录数
	 *
	 * @param queryUserName
	 * @param queryUserRole
	 * @return
	 */
	@Override
	public int getUserCount(String queryUserName, Integer queryUserRole) throws Exception {
		return userMapper.getUserCount(queryUserName, queryUserRole);
	}

	/**
	 * 根据userCode查询出User
	 *
	 * @param userCode
	 * @return
	 */
	@Override
	public User selectUserCodeExist(String userCode) {
		User user=null;
		try {
			user = userMapper.getLoginUser(userCode);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return user;
	}

	/**
	 * 根据ID删除user
	 *
	 * @param delId
	 * @return
	 */
	@Override
	public boolean deleteUserById(Integer delId) throws Exception {
		boolean flag=false;
		try {
			int count=userMapper.deleteUserById(delId);
			if (count>0){
				flag=true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

	/**
	 * 根据ID查找user
	 *
	 * @param id
	 * @return
	 */
	@Override
	public User getUserById(String id) {
		User user=null;

		try {
			user=userMapper.getUserById(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return user;
	}

	/**
	 * 修改用户信息
	 *
	 * @param user
	 * @return
	 */
	@Override
	public boolean modify(User user) {
		boolean flag=false;
		try {
			int count=userMapper.modify(user);
			if (count>0){
				flag=true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

	/**
	 * 根据userId修改密码
	 *
	 * @param id
	 * @param pwd
	 * @return
	 */
	@Override
	public boolean updatePwd(int id, String pwd) {
		boolean flag=false;
		try {
			int count=userMapper.updatePwd(id,pwd);
			if (count>0){
				flag=true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

}
